// import { concat } from 'core-js/core/array'
import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'Layout',
  component: () => import('../views/Layout/index.vue'),
  children: [{
    path: '',
    name: 'Home',
    component: () => import('../views/Home/index.vue')
  }, {
    path: 'artcategory',
    name: 'ArtCategory',
    component: () => import('../views/ArtCategory/index.vue')
  }, {
    path: 'artlist',
    name: 'ArtList',
    component: () => import('../views/ArtList/index.vue')
  }, {
    path: 'userinfo',
    name: 'UserInfo',
    component: () => import('../views/UserInfo/index.vue')
  }, {
    path: 'changavatar',
    name: 'ChangAvater',
    component: () => import('../views/changAvatar/index.vue')
  }, {
    path: 'resetpwd',
    name: 'resetPwd',
    component: () => import('../views/resePwd/index.vue')
  }]
},
{
  path: '/login',
  name: 'Login',
  // route level code-splitting
  // this generates a separate chunk (about.[hash].js) for this route
  // which is lazy-loaded when the route is visited.
  component: () => import('../views/Login/index.vue')
}, {
  path: '/reg',
  name: 'reg',
  component: () => import('../views/Reg/index.vue')
}
]

const router = new VueRouter({
  routes
})
// 4.1定义白名单,通过includes的方法
const whiteList = ['/login', '/reg']
// 4路由守卫
router.beforeEach((to, form, next) => {
  // console.log(to) 规定从哪里路由进去
  // console.log(form) 从那个路由出来
  // console.log(next) 正确放行
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
